<template>
  <div class="comment-list">
    <van-cell title="全部评论"></van-cell>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <CommentItem
        v-for="(comment, index) in list"
        :key="index"
        :comment="comment"
      ></CommentItem>
    </van-list>
  </div>
</template>

<script>
import { getComments } from '@/api/comments'
import CommentItem from '@/components/comment-item'
export default {
  props: {
    source: {
      type: [Number, String, Object],
      required: true
    },
    list: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      loading: false,
      finished: false,
      offset: null,
      limit: 10
    }
  },
  components: {
    CommentItem
  },
  created() {
    this.onLoad()
  },
  methods: {
    async onLoad() {
      const { data } = await getComments({
        // type: this.isComment ? 'c' : 'a', // 查看文章评论：a，查看评论回复：c
        type: 'a',
        source: this.source,
        offset: this.offset,
        limit: this.limit
      })
      console.log(data)
      this.$emit('updateCommentCount', data.data.total_count)
      const { results } = data.data
      this.list.push(...results)
      this.loading = false
      if (results.length) {
        this.offset = data.data.last_id
      } else {
        this.finished = true
      }
    }
  }
}
</script>

<style lang="">
</style>